//
//  LBTabBarController.m
//  XianYu
//
//  Created by li  bo on 16/5/28.
//  Copyright © 2016年 li  bo. All rights reserved.
//

#import "LBTabBarController.h"
#import "LBNavigationController.h"

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
//#import "LBHomeViewController.h"
//#import "LBMineViewController.h"
//#import "LBpostViewController.h"
//#import "LBMessageViewController.h"
#import "BaseViewController.h"
#import "LBTabBar.h"
#import "UIImage+Image.h"


@interface LBTabBarController ()<LBTabBarDelegate>
{
    NSBundle *bundle;
}

@end

@implementation LBTabBarController

#pragma mark - 第一次使用当前类的时候对设置UITabBarItem的主题
+ (void)initialize
{
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    
    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = [UIColor clearColor];//没有选中
    dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:11];

    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = [UIColor clearColor];
    dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:11];

    [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    
    


}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpAllChildVc];

//    //创建自己的tabbar，然后用kvc将自己的tabbar和系统的tabBar替换下
//    LBTabBar *tabbar = [[LBTabBar alloc] init];
//    tabbar.myDelegate = self;
//    //kvc实质是修改了系统的_tabBar
//    [self setValue:tabbar forKeyPath:@"tabBar"];

//  [[UITabBar appearance] setBackgroundColor:[UIColor redColor]];
//    [[UITabBar appearance] setBarTintColor:[UIColor redColor]]; //修改背景色
}


#pragma mark - ------------------------------------------------------------------
#pragma mark - 初始化tabBar上除了中间按钮之外所有的按钮

- (void)setUpAllChildVc
{


    FirstViewController *firstVC = [[FirstViewController alloc] init];
    firstVC.state = HomePageState;
    [self setUpOneChildVcWithVc:firstVC Image:@"firstImage" selectedImage:@"firstSelectedImage" title:@""];

    SecondViewController *secondVC = [[SecondViewController alloc] init];
     bundle = [InternationalControl bundle];
    NSString *lan = [InternationalControl userLanguage];
    NSString *str ;
    if([lan isEqualToString:@"en"]){//判断当前的语言，进行改变
        str = @"Messages";
    }
    else{
         str = @"消息";
    }
    
    [self setUpOneChildVcWithVc:secondVC Image:@"SecImage" selectedImage:@"SecSelectedImage" title:str];
    
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    [self setUpOneChildVcWithVc:thirdVC Image:@"ThirdImage" selectedImage:@"ThirdSelectedImage" title:@""];

//    LBMessageViewController *MessageVC = [[LBMessageViewController alloc] init];
//    [self setUpOneChildVcWithVc:MessageVC Image:@"message_normal" selectedImage:@"message_highlight" title:@"消息"];
//
//    LBMineViewController *MineVC = [[LBMineViewController alloc] init];
//    [self setUpOneChildVcWithVc:MineVC Image:@"account_normal" selectedImage:@"account_highlight" title:@"我的"];


}

#pragma mark - 初始化设置tabBar上面单个按钮的方法

/**
 *  @author li bo, 16/05/10
 *
 *  设置单个tabBarButton
 *
 *  @param Vc            每一个按钮对应的控制器
 *  @param image         每一个按钮对应的普通状态下图片
 *  @param selectedImage 每一个按钮对应的选中状态下的图片
 *  @param title         每一个按钮对应的标题
 */
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
    LBNavigationController *nav = [[LBNavigationController alloc] initWithRootViewController:Vc];


//    Vc.view.backgroundColor = [self randomColor];

    UIImage *myImage = [UIImage imageNamed:image];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    //tabBarItem，是系统提供模型，专门负责tabbar上按钮的文字以及图片展示
    Vc.tabBarItem.image = myImage;

    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    Vc.tabBarItem.selectedImage = mySelectedImage;

    Vc.tabBarItem.title = title;

    Vc.navigationItem.title = title;
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    // 给自定义 View 设置颜色
    bgView.backgroundColor = [UIColor colorWithRed:39/255.0 green:39/255.0 blue:39/255.0 alpha:1];
    // 将自定义 View 添加到 tabBar 上
    [self.tabBar insertSubview:bgView atIndex:0];
    

    [self addChildViewController:nav];
    
//    CGSize indicatorImageSize = CGSizeMake(self.tabBar.bounds.size.width/self.tabBar.items.count, self.tabBar.bounds.size.height);
//    self.tabBar.selectionIndicatorImage = [self drawTabbarBgColor:indicatorImageSize ];
    
    CGSize indicatorImageSize=CGSizeMake([[UIScreen mainScreen] bounds].size.width/3,self.tabBar.bounds.size.height);
    
    self.tabBar.selectionIndicatorImage=[self drawTabBarItemBackgroundUmageWithSize:indicatorImageSize];

    
}
-(UIImage*)drawTabBarItemBackgroundUmageWithSize:(CGSize)size

{
    
    //开始图形上下文
    
    UIGraphicsBeginImageContext(size);
    
    //获得图形上下文
    
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    
    CGContextSetRGBFillColor(ctx,0,0,0,1);
    if (kIs_Iphone_X)
    {
//    CGContextFillRect(ctx,CGRectMake(0,0,size.width,size.height));
    }
    else
    {
      CGContextFillRect(ctx,CGRectMake(0,0,size.width,size.height));
    }
    CGRect rect=CGRectMake(0,0,size.width,size.height);
    
    CGContextAddEllipseInRect(ctx, rect);
    
    CGContextClip(ctx);
    
    UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
    
    [image drawInRect:rect];
    
    UIGraphicsEndImageContext();
    
    return image;
    
}


#pragma mark - ------------------------------------------------------------------
#pragma mark - LBTabBarDelegate
//点击中间按钮的代理方法
- (void)tabBarPlusBtnClick:(LBTabBar *)tabBar
{


//    LBpostViewController *plusVC = [[LBpostViewController alloc] init];
//    plusVC.view.backgroundColor = [self randomColor];
//
//    LBNavigationController *navVc = [[LBNavigationController alloc] initWithRootViewController:plusVC];
//
//    [self presentViewController:navVc animated:YES completion:nil];



}


- (UIColor *)randomColor
{
    CGFloat r = arc4random_uniform(256);
    CGFloat g = arc4random_uniform(256);
    CGFloat b = arc4random_uniform(256);
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];

}

@end
